package james.gan.bean;

import java.util.List;

import james.gan.core.BeanDefinition;
import james.gan.utils.StringUtils;

/**
 * @Description: �򵥵�Bean����ʵ��
 * @author James
 * @date 2017/12/26
 */
public class DefaultBeanDefinition implements BeanDefinition {
  /**
   * Bean ID
   */
  private String name;
  /**
   * ����
   */
  private final Class<?> beanClass;

  /**
   * �������
   */
  private List<Object> args;

  public DefaultBeanDefinition(Class<?> beanClass) {
    super();
    this.beanClass = beanClass;
  }

  public Object getBeanInstace() {
    try {
      if (args == null || args.isEmpty()) {
        return beanClass.newInstance();
      } else {
        return beanClass
            .getConstructor(args.stream().map(i -> i.getClass()).toArray(i -> new Class<?>[i]))
            .newInstance(args);
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  public Class<?> getBeanClass() {
    return beanClass;
  }

  public boolean matchClass(Class<?> targetClass) {
    return false;
  }

  public String getBeanID() {
    if (StringUtils.NotBlank(name) || StringUtils
        .NotBlank((this.name = StringUtils.firstLowerCaseString(beanClass.getSimpleName())))) {
    }
    return name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List<Object> getArgs() {
    return args;
  }

  public void setArgs(List<Object> args) {
    this.args = args;
  }
}
